<?php 

//先从POST里面获取参数，如果POST里面没有则从GET里面获取
function extractReqVar($varname)
{
	if( isset($_GET[$varname]) )
    	$v=$_GET[$varname];	 
    if( isset($_POST[$varname]) )
    	$v=$_POST[$varname];
    
    return isset($v)?$v:null;
}

//生成GUID
function makeGuid()
{
	$s =	md5(time() . mt_rand(1,1000000));
	$r=substr($s, 0,8).'-'.substr($s,8, 4).'-'.substr($s,12, 4).'-'.substr($s,16, 4).'-'.substr($s,20, 32);

	return strtolower ($r);
}

function curlPost($url,$post_data){
	$ch = curl_init(); //初始化curl
	curl_setopt($ch, CURLOPT_URL, $url);//设置链接
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
	$output = curl_exec($ch);
	curl_close($ch);
	return $output;
}

function curlGet($url){
	$ch = curl_init(); //初始化curl
	curl_setopt($ch, CURLOPT_URL, $url);//设置链接
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
	curl_setopt($ch, CURLOPT_HEADER, 0);
	$output = curl_exec($ch);
	curl_close($ch);
	return $output;
}

function checkIsLogin(){
	$cookie_token = $_COOKIE["CCUSER"];
	$user = new UserClient();
	if($cookie_token){
		$online_info = json_decode($user->online($cookie_token),true);
		return $online_info['status'] == 0 ? true : false;
	}
	else{
		return false;
	}
}

function getVmnameById($id){
	$db = new DB();
	$vmname  = $db->getOne('select vmname from cc_vm where id="'.$id.'"');
	$db->close();
	return  $vmname['vmname'];
}
//不同的用户有不同的权限
function getMenu($token){
    $db =new DB();
    $role_id=$db->getOne("SELECT role_id from chu_user where token='".$token."'");
    $sql_menu ="SELECT * from chu_menu where id in (SELECT menu_id from chu_role_menu where role_id=".$role_id['role_id'].") order by pid";
    $menu=$db->query($sql_menu);
    
    $menu=menu_merge($menu);
    $array=array();
	foreach ($menu as $key => $value) {
	    $arr=array();
	    $arr['text']=$value["name"];
	    $arr['url']=$value["url"];
	    $arr['icon_class']=$value["icon_class"];
	    $arr2=array();
	    foreach ($value['children'] as $k => $v) {
	        $arr3=array();
	        $arr3['text']=$v['name'];
	        $arr3['url']=$v['url'];
	        $arr2[$v["key_name"]]=$arr3;
	    }
	    $arr['children']=$arr2;
	    $array[$value['key_name']]=$arr;
	}
	$db->close();
    return  $array;
}

function menu_merge($menu,$pid=0){
	$arr=array();
	foreach ($menu as $v) {
		if($v['pid']==$pid){
			$v['children']=menu_merge($menu,$v['id']);
			$arr[]=$v;
		}
	}
	return $arr;
}
function menu_merge2($menu,$access=null,$pid=0){
	//p($access);die;
	$arr=array();
	foreach ($menu as $v) {
		if(is_array($access)){
			$v['access']=(in_array($v['id'], $access))? 1: 0;
		}
		if($v['pid']==$pid){
			$v['children']=menu_merge2($menu,$access,$v['id']);
			$arr[]=$v;
		}
	}
	return $arr;
}
function p ($arr){ 
	echo '<pre>' . print_r($arr,true) . '<pre>' ;
}
//得到地区信息
function getRegion($region_id,$region_type){
	$db=new DB();
	//拿省一级的
	if($region_type==1||$region_type=='1'){
		//由于客户需求，现在只需要湖北的
		$data=$db->query("SELECT * from chu_region where region_id=13");
		//$data=$db->query("SELECT * from chu_region where region_type=".$region_type);
		return $data;
	}else{
		$data=$db->query("SELECT * from chu_region where region_type=".$region_type." and parent_id=".$region_id);
		return $data;
	}
	
}

//通过区Id得到全网点信息
 function getOutlet($id,$db){
	$region=$db->getOne("SELECT * from chu_region where region_id=".$id);
	$region_name=$region['region_name'];
	$parent_id=$region['parent_id'];
	if($parent_id>=2)
	{
		$p=getOutlet($parent_id,$db);
		$region_name=$p."--".$region_name;
	}
	
	return $region_name;
}

